﻿using Kxx_Common.INI;
using Kxx_Common.Tools;
using Kxx_Domain.DNS;
using Kxx_Domain.Email;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kxx_Save.DNS
{
    public class BascConfig
    {
        private static INIFile ini = new INIFile(AppDomain.CurrentDomain.BaseDirectory + @"bascconfig.ini");

        private static readonly object apiObj = new object();
        private static APIConfig apiConfig;

        private static readonly object qqEmailObj = new object();
        private static QQEmailConfig qqEmailConfig;

        private static readonly object domainObj = new object();
        private static List<DomainRecordTaskModel> domainRecordTaskModels;

        /// <summary>
        /// 阿里API配置信息
        /// </summary>
        public static APIConfig ALiInfo
        {
            get
            {
                if (apiConfig == null)
                {
                    lock (apiObj)
                    {
                        if (apiConfig == null)
                        {
                            string content = ini.IniReadValue("DNS", "API");
                            var obj = ConvertJson.DeserializeJsonToObject<APIConfig>(content);

                            if (obj == null) apiConfig = new APIConfig();
                            else apiConfig = obj;
                        }
                    }
                }
                return apiConfig;
            }
            set
            {
                string content = ConvertJson.SerializeObject(value);
                ini.IniWriteValue("DNS", "API", content);

                apiConfig = value;
            }
        }

        /// <summary>
        /// QQ邮箱配置信息
        /// </summary>
        public static QQEmailConfig QQEmailInfo
        {
            get
            {
                if (qqEmailConfig == null)
                {
                    lock (apiObj)
                    {
                        if (qqEmailConfig == null)
                        {
                            string content = ini.IniReadValue("Email", "QQAPI");
                            var obj = ConvertJson.DeserializeJsonToObject<QQEmailConfig>(content);

                            if (obj == null) qqEmailConfig = new QQEmailConfig();
                            else qqEmailConfig = obj;
                        }
                    }
                }
                return qqEmailConfig;
            }
            set
            {
                string content = ConvertJson.SerializeObject(value);
                ini.IniWriteValue("Email", "QQAPI", content);

                qqEmailConfig = value;
            }
        }

        /// <summary>
        /// 解析任务列表
        /// </summary>
        public static List<DomainRecordTaskModel> DomainRecordTask
        {
            get
            {
                if (domainRecordTaskModels == null)
                {
                    lock (domainObj)
                    {
                        if (domainRecordTaskModels == null)
                        {
                            string content = ini.IniReadValue("DNS", "DomainList");
                            var obj = ConvertJson.DeserializeJsonToList<DomainRecordTaskModel>(content);

                            if (obj == null) domainRecordTaskModels = new List<DomainRecordTaskModel>();
                            else domainRecordTaskModels = obj;
                        }
                    }
                }

                return domainRecordTaskModels;
            }
            set
            {
                string content = ConvertJson.SerializeObject(value);
                ini.IniWriteValue("DNS", "DomainList", content);

                domainRecordTaskModels = value;
            }
        }

        private static void APIInit()
        {
            lock (apiObj)
            {
                string content = ini.IniReadValue("DNS", "API");
                var obj = ConvertJson.DeserializeJsonToObject<APIConfig>(content);

                if (obj != null) apiConfig = obj;
            }
        }

        private static void DomainInit()
        {
            lock (domainObj)
            {
                string content = ini.IniReadValue("DNS", "DomainList");
                var obj = ConvertJson.DeserializeJsonToList<DomainRecordTaskModel>(content);

                if (obj != null) domainRecordTaskModels = obj;
            }
        }

        /// <summary>
        /// 刷新配置
        /// </summary>
        public static void Reload()
        {
            APIInit();
            DomainInit();
        }
    }
}
